﻿using TPXSDK.NET.Mission.Instruction;

namespace TPXSDK.NET.Mission;

/// <summary>
/// 分词器管理器
/// </summary>
internal class TokenizerAdmin
{
    /// <summary>
    /// 词元管理器构造函数
    /// </summary>
    public TokenizerAdmin(FlyCompiler cmp)
    {
        Compiler = cmp;

        // 添加伪指令处理对象
        Add(new PseudoFastWp(this));

        // 添加真指令处理对象
        Add(new InstTakeoff(this));
        Add(new InstFlyto(this));

        Add(new InstClimb(this));
        Add(new InstWaitClimb(this));
        Add(new InstWaitSpin(this));
        Add(new InstWaitFmt(this));

        Add(new InstAltKind(this));

        // 载荷设备控制指令
        Add(new InstCamCtrl(this));
        Add(new InstPWMCtrl(this));
        Add(new InstPODCtrl(this));
        Add(new InstIOCtrl(this));

        Add(new InstCircleRight(this));
        Add(new InstCircleLeft(this));
        Add(new InstCircleSet(this));

        Add(new InstHover(this));
        Add(new InstLand(this));

        Add(new InstDelay(this));
        Add(new InstRepeat(this));

        Add(new InstSetSpeed(this));
        Add(new InstSetLandYaw(this));
        Add(new InstSetLandAlt(this));        
        Add(new InstSetWpYaw(this));
    }

    /// <summary>
    /// 内部关联编译器
    /// </summary>
    public FlyCompiler Compiler { get; }

    /// <summary>
    /// 根据关键词查找词元处理器
    /// </summary>
    public Tokenizer? Get(string keyName)
    {
        //为了兼容代码大小写混合方式，在此进行转换处理
        keyName = keyName.ToLower();
        return _tokenizerNameDict.TryGetValue(keyName, out Tokenizer? value) ? value : null;
    }

    /// <summary>
    /// 根据关键ID查找词元处理器
    /// </summary>
    public Tokenizer? Get(int keyValue)
    {
        return _tokenizerValueDict.TryGetValue(keyValue, out Tokenizer? value) ? value : null;
    }

    /// <summary>
    /// 增加词元处理器
    /// </summary>
    private void Add(Tokenizer tokenizer)
    {
        _tokenizerNameDict.Add(tokenizer.KeyName, tokenizer);
        _tokenizerValueDict.Add(tokenizer.KeyValue, tokenizer);
        Tokenizers.Add(tokenizer);
    }

    #region 成员变量
    private readonly Dictionary<string, Tokenizer> _tokenizerNameDict = [];
    private readonly Dictionary<int, Tokenizer> _tokenizerValueDict = [];
    /// <summary>
    /// 词元列表
    /// </summary>
    public IList<Tokenizer> Tokenizers { get; private set; } = [];
    #endregion
}
